package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.LeaderprofitdetailService;
import com.youi.service.LeaderprofitdetailVoService;
import com.youi.entity.Leaderprofit;
import com.youi.entity.Leaderprofitdetail;
import com.youi.entity.vo.LeaderprofitdetailVo;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 团长佣金流水明细控制层
* @author Administrator
*/
@Controller
public class LeaderprofitdetailController  extends HttpRequestManage{

		@Autowired
		private LeaderprofitdetailService leaderprofitdetailService;
		@Autowired
		private LeaderprofitdetailVoService leaderprofitdetailVoService;

		/**
		 *添加或编辑团长佣金流水明细
		*/
		@ResponseBody
		@RequestMapping("addOrEditLeaderprofitdetail")
		public String addOrEditLeaderprofitdetail(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Leaderprofitdetail leaderprofitdetail=null;
			if (id!=null) {
				leaderprofitdetail=leaderprofitdetailService.get(id);
			}else {
				leaderprofitdetail=new Leaderprofitdetail();
				leaderprofitdetail.setDelStatus(DelStatus.NODEL);
				leaderprofitdetail.setGmtCreated(new Date());
			}
			Long leaderId=optLong("leaderId",request);
			if (leaderId!=null){
				leaderprofitdetail.setLeaderId(leaderId);
			}
			Integer type=optInteger("type",request);
			if (type!=null){
				leaderprofitdetail.setType(type);
			}
			Double amount=optDouble("amount",request);
			if (amount!=null){
				leaderprofitdetail.setAmount(amount);
			}
			String description=optString("description",request);
			if (description!=null){
				leaderprofitdetail.setDescription(description);
			}
			String orderNo=optString("orderNo",request);
			if (orderNo!=null){
				leaderprofitdetail.setOrderNo(orderNo);
			}
			leaderprofitdetail.setGmtModify(new Date());
			leaderprofitdetailService.saveOrUpdate(leaderprofitdetail);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除团长佣金流水明细
		*/
		@ResponseBody
		@RequestMapping("delLeaderprofitdetailByIds")
		public String delLeaderprofitdetailByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Leaderprofitdetail> leaderprofitdetailList= leaderprofitdetailService.getByParams(params);
			if (leaderprofitdetailList==null||leaderprofitdetailList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Leaderprofitdetail leaderprofitdetail : leaderprofitdetailList) {
				leaderprofitdetail.setDelStatus(DelStatus.DEL);
			}
			leaderprofitdetailService.saveOrUpdateAll(leaderprofitdetailList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-121
		 * 查询佳选官资金流水列表
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getLeaderprofitdetail")
		public String getLeaderprofitdetail(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			String start=optString("start",request);
			String end=optString("end",request);
			String orderNo=optString("orderNo", request);
			String phone=optString("phone", request);			
			List<LeaderprofitdetailVo> leaderprofitdetailList=leaderprofitdetailVoService.getLeaderprofitdetailVoList(start, end, orderNo, phone, pms,(pageIndex-1)*pageNum, pageNum);
			if (leaderprofitdetailList==null||leaderprofitdetailList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=leaderprofitdetailVoService.getLeaderprofitdetailVoCount(start, end, orderNo, phone, pms);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, leaderprofitdetailList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}
		
		/**
		 * I-122
		 * 导出佳选官资金流水excel
		*/
		@ResponseBody
		@RequestMapping("toExcelLeaderprofitdetail")
		public String toExcelLeaderprofitdetail(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{			
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			String start=optString("start",request);
			String end=optString("end",request);
			String orderNo=optString("orderNo", request);
			String phone=optString("phone", request);			
			List<LeaderprofitdetailVo> leaderprofitdetailList=leaderprofitdetailVoService.getLeaderprofitdetailVoList(start, end, orderNo, phone, pms,null, null);
			if (leaderprofitdetailList==null||leaderprofitdetailList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			modelMap.put("url", leaderprofitdetailVoService.toExcelLeaderprofitdetailVo(request, leaderprofitdetailList));
			modelMap.put(CommonInfoUtil.JSONCODE, CommonInfoUtil.SUCCESS);
			modelMap.put(CommonInfoUtil.JSONMSG, "操作成功");	//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * I-123
		 * 查询佳选官资金流水汇总
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getLeaderprofitdetailSum")
		public String getLeaderprofitdetailSum(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{			
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			String start=optString("start",request);
			String end=optString("end",request);
			String orderNo=optString("orderNo", request);
			String phone=optString("phone", request);			
			Leaderprofit leaderprofit=leaderprofitdetailVoService.getLeaderprofitdetailSum();			
			modelMap.put("totalAmount", leaderprofit.getTotalAmount());
			modelMap.put("withdres", leaderprofit.getWithdres());
			modelMap.put("frozen", leaderprofit.getFrozen());
			modelMap.put("balance", leaderprofit.getBalance());
			Double income=income=leaderprofitdetailVoService.getLeaderprofitdetailByType(0, start, end, orderNo, phone, pms);
			if (income==null) {
				income=0.00;
			}
			modelMap.put("income", income);
			Double withdraw=leaderprofitdetailVoService.getLeaderprofitdetailByType(1, start, end, orderNo, phone, pms);
			if (withdraw==null) {
				withdraw=0.00;
			}
			modelMap.put("withdraw", withdraw);
			Double refund=leaderprofitdetailVoService.getLeaderprofitdetailByType(2, start, end, orderNo, phone, pms);
			if (refund==null) {
				refund=0.00;
			}
			modelMap.put("refund", refund);
			modelMap.put(CommonInfoUtil.JSONMSG, "操作成功");	//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}
		
		/**
		 *根据id查询团长佣金流水明细
		*/
		@ResponseBody
		@RequestMapping("getLeaderprofitdetailById")
		public String getLeaderprofitdetailById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Leaderprofitdetail leaderprofitdetail=leaderprofitdetailService.get(id);
			if (leaderprofitdetail==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",leaderprofitdetail);//jsonObject	json对象
		}

		
		
		/**
		 * 查询所有团长佣金流水明细
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllLeaderprofitdetail")
		public String getAllLeaderprofitdetail(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Leaderprofitdetail> leaderprofitdetailList= leaderprofitdetailService.getByParams(null);
			if (leaderprofitdetailList==null||leaderprofitdetailList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",leaderprofitdetailList);//jsonObjectList	json对象集合
		}
}
